

local lang_utils = require("mvc.utils.lang_utils");

local cookie = {
	
	domain = nil,
	path = nil,
	name = nil,
	value = nil,
	max_age = nil
	
};

--[[
	实例化cookie
	@domain    域名,eg:.test.com
	@path      路径
	@name      cookie名称
	@value     cookie值
	@max_age   有效时间，单位：秒
]]
function cookie:new(domain, path, name, value, max_age)
	local o = {};
	setmetatable(o, self);
	self.__index = self;
	self.domain = domain;
	self.path = path;
	self.name = name;
	self.value = value;
	self.max_age = max_age;
	return o;
end


function cookie:set_domain(domain)
	self.domain = domain;
end

function cookie:get_domain()
	return self.domain;
end


function cookie:set_path(path)
	self.path = path;
end

function cookie:get_path()
	return self.path;
end

function cookie:set_name(name)
	self.name = name;
end

function cookie:get_name()
	return self.name;
end

function cookie:set_value(value)
	self.value = value;
end

function cookie:get_value()
	return self.value;
end

function cookie:set_max_age(max_age)
	self.max_age = max_age;
end

function cookie:get_max_age()
	return self.max_age;
end

--[[
	将Cookie实例输出为字符串
]]
function cookie:to_string()
	return lang_utils.to_string(self);
end

--[[
	转换为用于写入浏览器的字符串
]]
function cookie:to_cookie_string()
	local ci = "";
	ci = ci .. self.name .. "=" .. self.value .. "; ";
	ci = ci .. "Expires=" .. ngx.cookie_time((ngx.time() + self.max_age)) .. "; ";
	ci = ci .. "Path=".. self.path .. "; ";
	ci = ci .. "Domain=".. self.domain .. "; ";
	return ci;
end

return cookie;

